/**
 * Created by Administrator on 2017/6/10.
 */
var nam=document.querySelector(".nam");//登录用户名
var pass=document.querySelector(".pass");//登录密码
var enroll=document.querySelector(".enroll");//注册按钮
var register=document.querySelector(".register");//登录窗口
var have=document.querySelector(".have");//注册窗口
var enter=document.querySelector(".enter");//登录按钮
var yes=document.querySelector(".yes");//确定按钮
var userN=document.querySelector(".userN");//注册用户名输入框
var passW=document.querySelector(".passW");//注册密码输入框
enter.addEventListener("click",clickHandler);//给登录按钮添加点击事件
enroll.addEventListener("click",clickHandler2);//给注册按钮添加点击事件
yes.addEventListener("click",clickHandler3);//给确定按钮添加点击事件


/*点击登录按钮，登录成功后跳转到添加信息的表格*/
var xhr=cohenLee.createXHR();
function clickHandler(e){
xhr.open("post","http://localhost:6500/users/login");
xhr.setRequestHeader("Content-type", "application/json");
xhr.send(JSON.stringify({username:nam.value,password:pass.value}));
xhr.onreadystatechange=function () {
    if(xhr.readyState==4){
        if(xhr.status>=200&&xhr.status<300||xhr.status==304){
            /*用本地存储保存用户名和密码*/
            localStorage.setItem("a",JSON.stringify({user:nam.value,pwd:pass.value}));
            window.open("data.html");
        }
     }
  };
    if(nam.value==""||pass.value==""){
        window.alert("请将信息填写完整");
    }
}
/*点击注册按钮，出现注册界面*/
function clickHandler2(e) {
    register.style.display="none";
    have.style.display="block";
}

/*填写完注册信息后，同步添加到后台里*/
function clickHandler3(e) {
    xhr.open("post","http://localhost:6500/users");
    xhr.setRequestHeader("Content-type", "application/json");
    xhr.send(JSON.stringify({username:userN.value,password:passW.value}));
    xhr.onreadystatechange=function () {
        if(xhr.readyState==4){
            if(xhr.status>=200&&xhr.status<300||xhr.status==304){
                console.log(xhr.responseText)
            }
        }
    };
    register.style.display="block";
    have.style.display="none";
}